-- Pierwotny właściciel i twórca kodu: AmaMa
-- Zakaz używania / edycji kodu bez wcześniejszej zgody ze strony właściciela
-- Zakaz wykorzystywania całego lub fragmentów kodu
-- W przypadku złamania powyzszych zasad sprawa zostanie skierowana do sądu pod zarzutem: Naruszenia majątkowych praw autorskich
-- Pierwotnie pełne prawo do używania tego kodu posiada tylko AmaMa

loadstring(exports.dgs:dgsImportFunction())()

local sw,sh = guiGetScreenSize()
local px,py = sw/1366,sh/768
cmtaPAINTJOBY = false
font = dgsCreateFont( ":cmta-pobieraniezasobow/OSB.ttf", 12*px )

local pjid=0

painjobs = {
    [1]="5",
    [2]="5",
    [3]="5",
    [4]="5",
    [5]="5",
    [6]="5",
    [7]="5",
    [8]="5",
    [9]="5",
    [10]="5",
    [11]="5",
    [12]="5",
    [13]="5",
    [14]="5",
    [15]="5",
    [16]="5",
    [17]="5",
    [18]="5",
    [19]="5",
    [20]="5",
    [21]="5",
    [22]="5",
    [23]="5",
    [24]="9",
    [25]="9",
    [26]="9",
    [27]="9",
    [28]="9",
    [29]="9",
    [30]="9",
    [31]="9",
    [32]="14",
    [33]="14",
    [34]="14",
    [35]="14",
    [36]="14",
    [37]="14",
    [38]="17",
    [39]="17",
    [40]="19",
    [41]="19",
    [42]="19",
    [43]="19",
    [44]="19",
    [45]="22",
    [46]="22",
    [47]="22",
    [48]="22",
    [49]="22",
    [50]="22",
    [51]="24",
    [52]="24",
    [53]="24",
    [54]="25",
    [55]="25",
    [56]="28",
    [57]="29",
    [58]="30",
    [59]="32",
    [60]="32",
    [61]="35",
    [62]="35",
    [63]="35",
    [64]="35",
    [65]="35",
    [66]="35",
    [67]="35",
    [68]="35",
    [69]="35",
    [70]="35",
    [71]="40",
    [72]="40",
    [73]="40",
    [74]="40",
    [75]="40",
    [76]="41",
    [77]="41",
    [78]="42",
    [79]="42",
    [80]="42",
    [81]="42",
    [82]="42",
    [83]="42",
    [84]="42",
    [85]="43",
    [86]="43",
    [87]="43",
    [88]="44",
    [89]="44",
    [90]="44",
    [91]="44",
    [92]="47",
    [93]="48",
    [94]="48",
    [95]="48",
    [96]="48",
    [97]="48",
    [98]="49",
    [99]="49",
    [100]="49",
    [101]="49",
    [102]="49",
    [103]="49",
    [104]="49",
    [105]="49",
    [106]="49",
    [107]="49",
    [108]="50",
    [109]="50",
    [110]="50",
    [111]="50",
    [112]="50",
    [113]="50",
    [114]="50",
    [115]="50",
    [116]="50",
    [117]="50",
    [118]="50",
    [119]="50",
    [120]="50",
    [121]="50",
    [122]="50",
    [123]="50",
    [124]="50",
    [125]="50",
    [126]="50",
    [127]="50",
    [128]="50",
    [129]="50",
    [130]="50",
    [131]="50",
    [132]="50",
    [133]="50",
    [134]="50",
    [135]="50",
    [136]="50",
    [137]="50",
    [138]="50",
    [139]="50",
    [140]="50",
    [141]="50",
    [142]="50",
    [143]="50",
    [144]="50",
    [145]="50",
    [146]="50",
    [147]="50",
    [148]="50",
    [149]="50",
    [150]="50",
    [151]="50",
    [152]="50",
    [153]="50",
    [154]="50",
    [155]="50",
}



function gui()
    local rotx,roty,rotz = getElementRotation(getPedOccupiedVehicle(localPlayer))
    setElementRotation(getPedOccupiedVehicle(localPlayer),rotx,roty,rotz+1)
end

addEvent("paintjobs:showGUI", true)
addEventHandler("paintjobs:showGUI", resourceRoot, function()
    showCursor(true)
    pjid = 0
    cmtaPAINTJOBY = true
    rndRect = dgsCreateRoundRect(30,false,tocolor(0,0,0,190))  
    rect1 = dgsCreateImage(1038*px,150*py,309*px,368*py,rndRect,false)
    button = dgsCreateButton( 60*px, 160*py, 65*px, 40*py, "<", false,rect1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
    button2 = dgsCreateButton( 189*px, 160*py, 65*px, 40*py, ">", false,rect1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
    button3 = dgsCreateButton( 100*px, 250*py, 109*px, 40*py, "kup", false,rect1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
    button4 = dgsCreateButton( 100*px, 300*py, 109*px, 40*py, "zamknij", false,rect1,false,false,false,false,false,false,false,tocolor(90,90,90),tocolor(255,0,0,90))
    text = dgsCreateLabel(0*px,60*py,309*px,50*py,"#808080"..pjid.."/155\n#808080Wymagany level: #DD7900"..((painjobs[pjid] or 0)).."\n#808080Koszt: #00ff0018500 punktów",false,rect1)
    dgsSetFont( text, font )
    dgsSetProperty(text,"colorcoded",true)
    dgsSetProperty(text,"alignment",{"center","center"})
    addEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    x,y,z = getElementRotation(getPedOccupiedVehicle(localPlayer))
    setElementFrozen(getPedOccupiedVehicle(localPlayer),true)
end)

addEvent("paintjobs:hideGUI", true)
addEventHandler("paintjobs:hideGUI", resourceRoot, function()
    if cmtaPAINTJOBY == true then
    showCursor(false)
    cmtaPAINTJOBY = false
    destroyElement(rect1)
    destroyElement(rndRect)
    removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
    setElementRotation(getPedOccupiedVehicle(localPlayer),x,y,z)
    setElementFrozen(getPedOccupiedVehicle(localPlayer),false)
    refresh()
    end
end)

function prawo()
	pjid = pjid+1
	    local minpj,maxpj=1,155
	    if(pjid > maxpj)then
	        pjid = minpj
        end
        dgsSetText(text,"#808080"..pjid.."/155\n#808080Wymagany level: #DD7900"..((painjobs[pjid] or 0)).."\n#808080Koszt: #00ff0018500 punktów")
        vehTexture = dxCreateTexture( "files/"..pjid..".png" )
	    shader_cars, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
	    engineRemoveShaderFromWorldTexture ( shader_cars, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
	    engineApplyShaderToWorldTexture ( shader_cars, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
     	dxSetShaderValue ( shader_cars, "TX0", vehTexture ) 	
end

function lewo()
	pjid = pjid-1
	    local minpj,maxpj=1,155
	    if(pjid < minpj)then
	        pjid = maxpj
        end
        dgsSetText(text,"#808080"..pjid.."/155\n#808080Wymagany level: #DD7900"..((painjobs[pjid] or 0)).."\n#808080Koszt: #00ff0018500 punktów")
        vehTexture = dxCreateTexture( "files/"..pjid..".png" )
	    shader_cars, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
	    engineRemoveShaderFromWorldTexture ( shader_cars, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
	    engineApplyShaderToWorldTexture ( shader_cars, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
     	dxSetShaderValue ( shader_cars, "TX0", vehTexture ) 	
end



function onclick ( btn, state )
    if btn == "left" and state == "down" and cmtaPAINTJOBY == true then
        if source == button then
            lewo()
        end
        if source == button2 then
            prawo()
        end
        if source == button3 then
            if not isPedInVehicle(localPlayer) then return end
            if getElementData(localPlayer,"player:data").lvl < tonumber(painjobs[pjid]) then return end
            if getElementData(localPlayer,"player:data").points < 18500 then  return end
            if tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").shader) == tonumber(pjid) then return end
            triggerServerEvent('paintjobs:buy',resourceRoot,getPedOccupiedVehicle(localPlayer),pjid)       
        end
        if source == button4 then
            showCursor(false)
            cmtaPAINTJOBY = false
            destroyElement(rect1)
            destroyElement(rndRect)
            removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
            setElementRotation(getPedOccupiedVehicle(localPlayer),x,y,z)
            setElementFrozen(getPedOccupiedVehicle(localPlayer),false)
            refresh()
        end
    end
end

addEvent("paintjobs:addtexture", true)
addEventHandler("paintjobs:addtexture", resourceRoot, function(veh,id)
vehTexture = dxCreateTexture( "files/"..id..".png" )
shader_carsadd, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
engineRemoveShaderFromWorldTexture ( shader_carsadd, "vehiclegrunge256", veh )
engineApplyShaderToWorldTexture ( shader_carsadd, "vehiclegrunge256", veh )
dxSetShaderValue ( shader_carsadd, "TX0", vehTexture ) 	
showCursor(false)
cmtaPAINTJOBY = false
destroyElement(rect1)
destroyElement(rndRect)
removeEventHandler ( "onDgsMouseClick", getRootElement(), onclick)
setElementRotation(getPedOccupiedVehicle(localPlayer),x,y,z)
setElementFrozen(getPedOccupiedVehicle(localPlayer),false)
end)


function refresh()
   if tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").shader) == 0 then
    vehTexture = dxCreateTexture( "files/clear.png" )
    shader_carsref, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
    engineRemoveShaderFromWorldTexture ( shader_carsref, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
    engineApplyShaderToWorldTexture ( shader_carsref, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
    dxSetShaderValue ( shader_carsref, "TX0", vehTexture ) 	
   else
   if tonumber(pjid) ~= tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").shader) then
   vehTexture = dxCreateTexture( "files/"..tonumber(getElementData(getPedOccupiedVehicle(localPlayer),"vehicle:data").shader)..".png" )
   shader_carsref, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
   engineRemoveShaderFromWorldTexture ( shader_carsref, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
   engineApplyShaderToWorldTexture ( shader_carsref, "vehiclegrunge256", getPedOccupiedVehicle(localPlayer) )
   dxSetShaderValue ( shader_carsref, "TX0", vehTexture ) 	
   end
   end
end


addEventHandler("onClientResourceStart",resourceRoot,function()
for i,v in ipairs(getElementsByType("vehicle")) do
    if getElementData(v,"vehicle:data") then
local shaderid = tonumber(getElementData(v,"vehicle:data").shader)
if tonumber(shaderid) ~= 0 and getElementData(v,"vehicle:data") and getElementData(v,"vehicle:data").id then
		vehTexture = dxCreateTexture( "files/"..shaderid..".png" )
	    shader_carsstart, tec = dxCreateShader ( ":cmta-shadery/shader.fx" )
	    engineRemoveShaderFromWorldTexture ( shader_carsstart, "vehiclegrunge256", v )
	    engineApplyShaderToWorldTexture ( shader_carsstart, "vehiclegrunge256", v )
     	dxSetShaderValue ( shader_carsstart, "TX0", vehTexture ) 	
		end
end
end
end)